﻿-- Author      : Kurapica
-- Create Date : 7/16/2008 12:29
     
-- Model
do
	-- Check Version
	local version = 1
	if not IGAS:RegisterFile("IGAS.GUI.Widget.Model", version) then
		return
	end

    local getmetatable = getmetatable
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local CreateFrame = CreateFrame
    
	local _WidgetName = "Model"
    local _Base = "Frame"

	local _GUI = IGAS.GUI
	local _Widget = IGAS.GUI.Widget
    local _BaseFunc = IGAS.GUI.Base.Func
	local _PropertyType = IGAS.GUI.Base.PropertyType   
	
	-- ScriptType
	local _ScriptType = {
	}
    
	-- FuncProxy
	local _FuncProxy = {        
    }
    
	-- Property
	local _Property = {	
    	-- FogColor
		["FogColor"] = {
			["Set"] = function(self, colorTable)
				self.__UI:SetFogColor(colorTable.red, colorTable.green, colorTable.blue, colorTable.alpha)
			end,
			
			["Get"] = function(self)
				local colorTable = {}
				colorTable.red, colorTable.green, colorTable.blue, colorTable.alpha = self.__UI:GetFogColor()
				return colorTable 
			end,
			
			["Type"] = _PropertyType.Color,
		},
    
    	-- FogFar
		["FogFar"] = {
			["Set"] = function(self, value)
				self.__UI:SetFogFar(value)
			end,
			
			["Get"] = function(self)
				return self.__UI:GetFogFar() 
			end,
			
			["Type"] = _PropertyType.Number,
		},
   
    	-- FogNear
		["FogNear"] = {
			["Set"] = function(self, value)
				self.__UI:SetFogNear(value)
			end,
			
			["Get"] = function(self)
				return self.__UI:GetFogNear() 
			end,
			
			["Type"] = _PropertyType.Number,
		},   
         
    	-- ModelScale
		["ModelScale"] = {
			["Set"] = function(self, scale)
				self.__UI:SetModelScale(scale)
			end,
			
			["Get"] = function(self)
				return self.__UI:GetModelScale() 
			end,
			
			["Type"] = _PropertyType.Number,
		},   
		
    	-- Model
		["Model"] = {
			["Set"] = function(self, file)
				if file and type(file) == "string" and file ~= "" then
					self.__UI:SetModel(file)
				else
					self.__UI:ClearModel()
				end
			end,
			
			["Get"] = function(self)
				return self.__UI:GetModel() 
			end,
			
			["Type"] = _PropertyType.String,
		},
		
    	-- Position
		["Position"] = {
			["Set"] = function(self, table)
				self.__UI:SetPosition(table.x, table.y, table.z)
			end,
			
			["Get"] = function(self)
				local table = {}
				table.x, table.y, table.z = self.__UI:GetPosition()
				return table 
			end,
			
			["Type"] = _PropertyType.Color,
		},
		
    	-- Light
		["Light"] = {
			["Set"] = function(self, set)
				self.__UI:SetLight(set.enabled, set.omni, set.dirX, set.dirY, set.dirZ, set.ambIntensity, set.ambR, set.ambG, set.ambB, set.dirIntensity, set.dirR, set.dirG, set.dirB)
			end,
			
			["Get"] = function(self)
				local set = {}
				set.enabled, set.omni, set.dirX, set.dirY, set.dirZ, set.ambIntensity, set.ambR, set.ambG, set.ambB, set.dirIntensity, set.dirR, set.dirG, set.dirB = self.__UI:GetLight()
				return set 
			end,
			
			["Type"] = _PropertyType.Light,
		},
   }
	
    -- New  
    local _New = function(_parent,inheritFrame)
        -- New Frame
		local frame = CreateFrame(_WidgetName,nil,_BaseFunc.GetUI(_parent),inheritFrame)
		
        return frame
    end
	
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
    }
    
    _Widget[_WidgetName] = _GUI.NewWidget(_WidgetInfo)
    
	------------------------------------------------------
	--------------- Scan new function --------------------
    ------------------------------------------------------
	local _frame = CreateFrame(_WidgetName,nil,UIParent)
	
	local _oriMeta = getmetatable(_frame)
	_oriMeta = _oriMeta.__index
    
    local _func = _Widget[_WidgetName]["FuncProxy"]
	-- Other Function
	for _f in pairs(_oriMeta) do
		if not _func[_f] and type(_oriMeta[_f]) == "function" then
			_func[_f] = function(self, ...)
                return _BaseFunc.GetVals(self.__UI[_f](self.__UI, _BaseFunc.GetArgs(...)))
			end
		end
	end
	
	-- Clear
	_oriMeta = nil
    _frame:Hide()
	_frame = nil
end